// pages/invite/invite.js
const app = getApp();
var time = '';
Page({

  /**
   * 页面的初始数据
   */
	data: {
		images: {},
		imgurl: app.urlimg,
		num: '1',//还差的人数
		milliseconds: '',//倒计时的毫秒数
		time: '',//倒计时
		//参团人数
		user: [
			// { img: '../../../images/u213.png', },
			// { img: '../../../images/u213.png', },
		],
		dates: '',
		hours: '',
		// 保存状态
		state: '',
		//拼团成功
		success: true,
		//抽奖团购判断
		lotto: '',
		//抽奖团
		group_content: [
			// { img: '../../../images/u213.png', title: '骆驼户外抓绒衣骆驼户外抓绒衣户外抓绒衣骆驼户外抓绒衣',  new_price: '0.01', lod_price: '299.00', },
		],
		orderId: 0
	},
	// 广告图片大小（banner）
	imageLoad: function (e) {
		var $width = e.detail.width,    //获取图片真实宽度
			$height = e.detail.height,
			ratio = $width / $height;    //图片的真实宽高比例
		var viewWidth = 718,           //设置图片显示宽度，左右留有16rpx边距
			viewHeight = 718 / ratio;    //计算的高度值
		var image = this.data.images;
		//将图片的datadata-index作为image对象的key,然后存储图片的宽高值
		image[e.target.dataset.index] = {
			width: viewWidth,
			height: viewHeight
		}
		this.setData({
			images: image
		})
	},
	// 广告图片（banner）
	swiperItem: function (e) {
		var fid = e.currentTarget.id
		if (e.currentTarget.dataset.url !== '') {
			wx.redirectTo({
				url: e.currentTarget.dataset.url,
			})
		}
	},
  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		console.log(options);
		var that = this;
		var token_key = wx.getStorageSync('token_key');
		console.log(token_key);
		if (token_key == undefined || token_key == '') {
			app.getUserInfo(function () {
				load_fn();
			})
		} else {
			load_fn()
		}

		function load_fn() {
			wx.request({
				url: app.url + 'api_team/tearm_detail',
				method: 'GET',
				data: {
					order_id: options.order_id,
				},
				header: {
					'Content-Type': 'application/json',
					'token': wx.getStorageSync('token_key')
				},
				success: function (res) {
					console.log(res.data.result);
					that.setData({
						ad_top: res.data.result.ad_top,
					})

					//拼团推荐
					wx.request({
						url: app.url + 'api_team/recommend',
						data: {
							goods_id: res.data.result.goods.goods_id,
							// goods_id: 2829,
						},
						header: {
							'Content-Type': 'application/json',
							'token': wx.getStorageSync('token_key')
						},
						success: function (res) {
							console.log(res.data.result);
							that.setData({
								goods_lists: res.data.result,
							})

						},
						// 接口调用失败返回的函数
						fail: function (err) {
							console.log(err)
						}
					})


					that.setData({
						//lists: res.data.result
						state: res.data.result.goods.team_status,
						order: res.data.result.goods,
						team_first_1: res.data.result.team_first_1,
						team_first_2: res.data.result.team_first_2,
						orderId: options.order_id,
						milliseconds: res.data.result.goods.time_end_stamp
					})
				},
				//接口调用失败返回的函数
				fail: function (err) {
					console.log(err)
				}
			})
		}
	},
	//返回首页
	homeTAP: function () {
		wx.redirectTo(
			{
				url: '/pages/index/index',
				success: function (res) {
					clearInterval(time);
				}
			}
		)
	},
	//查看规则
	go_rule: function () {
		wx.navigateTo({
			url: '/pages/group/rule/index',
			success: function () {
				clearInterval(time);
			},
		})
	},
	// 进入商品详情页面(去开团)
	click: function (e) {
		wx.redirectTo({
			url: '../../../pages/group/groupdetail/index?id=' + e.currentTarget.dataset.index + '&regiment_id=' + e.currentTarget.dataset.regiment_id + "&group_type=" + e.currentTarget.dataset.group,
		})
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},

  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {
		var that = this;

		time = setInterval(function () {
			that.time(that.data.milliseconds);
		}, 1000);
	},
	// 获得毫秒数  转化  成小时、分钟、秒
	time: function (Time) {
		var that = this;
		Time -= 1000;
		var hours = parseInt(Time / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
		if (('' + hours).length == 1) {
			hours = '0' + hours;
		}
		var minutes = parseInt(Time / 1000 / 60 % 60, 10);//计算剩余的分钟 
		if (('' + minutes).length == 1) {
			minutes = '0' + minutes;
		}
		var seconds = parseInt(Time / 1000 % 60, 10);//计算剩余的秒数 
		if (('' + seconds).length == 1) {
			seconds = '0' + seconds;
		}
		var times = hours + ":" + minutes + ":" + seconds;//排列时间

		// console.log(times);
		that.setData({
			time: times,
			milliseconds: Time,
		})
	},
  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {
		clearInterval(time);
	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {
		clearInterval(time);
	},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function () {
		var that = this;
		wx.showNavigationBarLoading() //在标题栏中显示加载
		that.onLoad(that.data.orderId);
		//模拟加载
		setTimeout(function () {
			// complete
			wx.hideNavigationBarLoading() //完成停止加载
			wx.stopPullDownRefresh() //停止下拉刷新
		}, 1200);
	},

  /**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom: function () {

	},

  /**
   * 用户点击右上角分享
   */
	onShareAppMessage: function () {

	}
})